package com.wx.javacode.encap;

public class Encapsulation {
    public static void main(String[] args) {
        Person person = new Person("jack",30000,4000);
//        person.setName("jack");
//        person.setAge(3000);
//        person.setSalary(30000);
        person.info();
    }
}

class Person {
    public String name;
    private int age;
    private double salary;

    public Person(String name, int age, double salary) {
        this.name = name;
//        this.age = age;
        this.setAge(age);
        this.salary = salary;
    }

    public Person() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age > 0 && age <= 120) {
            this.age = age;
        } else {
            System.out.println("你输入的年龄不在1-120之间,默认给你18岁");
            this.age = 18;
        }
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public void info() {
        System.out.println("姓名:" + name + "\t" + "年龄:" + age + "\t" + "薪水:" + salary);
    }
}
